package com.chockqiu.view;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class LoopScrollingImagesView extends View {

    public LoopScrollingImagesView(Context context) {
        super(context);
    }

    public LoopScrollingImagesView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoopScrollingImagesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private float itemOffset = dpToPx(35f);
    private long duration = 20000L;
    private int dw;
    private int dh;
    private final Matrix mMetrics = new Matrix();
    private ValueAnimator mValueAnimator;
    private ArrayList<Drawable> mDrawables = new ArrayList<Drawable>();


    public final void setImages(List<Drawable> images) {
        if (!images.isEmpty()) {
            mDrawables.clear();
            mDrawables.addAll(images);
            post((Runnable) (new Runnable() {
                @Override
                public final void run() {
                    initParams();
                    startAnimation();
                }
            }));
        }
    }

    private void initParams() {
        Drawable d = mDrawables.get(0);
        dw = (int) (d.getIntrinsicWidth() * 1.0f / d.getIntrinsicHeight() * (getHeight() - itemOffset));
        dh = (int) (dw * 1.0f * d.getIntrinsicHeight() / d.getIntrinsicWidth());
    }

    private void startAnimation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (mValueAnimator != null) {
                mValueAnimator.cancel();
            }
            float maxOffsetX = mDrawables.size() * dw * 1.0F;
            mValueAnimator = ValueAnimator.ofFloat(0.0F, maxOffsetX);
            mValueAnimator.setDuration(duration);
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mMetrics.reset();
                    mMetrics.postTranslate(-((float) animation.getAnimatedValue()), 0f);
                    invalidate();
                }
            });

            mValueAnimator.setRepeatCount(-1);
            mValueAnimator.setRepeatMode(ValueAnimator.RESTART);
            mValueAnimator.setInterpolator((TimeInterpolator) (new LinearInterpolator()));
            mValueAnimator.start();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.concat(mMetrics);
        int offset = 0;
        int i = 0;
        int var4 = mDrawables.size() + 3;
        if (i <= var4) {
            while (true) {
                int index = i % mDrawables.size();
                Drawable drawable = (Drawable) mDrawables.get(index);
                int ot = i % 2 == 0 ? 0 : (int) itemOffset;
                int per = -((int) ((float) ot / 13.0F));
                drawable.setBounds(offset + per, ot, offset + dw + per, dh + ot);
                drawable.draw(canvas);
                offset += dw;
                if (i == var4) {
                    break;
                }
                ++i;
            }
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (!mDrawables.isEmpty()) {
            initParams();
            startAnimation();
        }
    }

    private float dpToPx(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, dp, getResources().getDisplayMetrics()) + 0.5F;
    }

    public final float getItemOffset() {
        return itemOffset;
    }

    public final void setItemOffset(float var1) {
        itemOffset = var1;
    }

    public final long getDuration() {
        return duration;
    }

    public final void setDuration(long var1) {
        duration = var1;
    }


}